<?php
/**
 * 管理中心· 权限管理设置模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\AjaxPage;
use Hoteladmin\Model\SetupModel;
class RoleController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
    
        $this->role_type= C('ROLE_TYPE');
        $this->assign('rtype',$this->role_type); //权限类型
    
        $timegap['beg'] = date('Y-m-d',strtotime("-1 year"));//1年前
        $timegap['end'] = date('Y-m-d',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取管理员列表的功能
     * @author:  XC跃
     * @date :2018-09-28
     +--------------------------------------------------------------------------
     */
    public function adminList(){
        $this->display();
    }
    public function ajaxAdminList(){
        //实例化相关表
        $adident = M('admin');
        // 搜索条件
        $condition = array();
        $sort_order = I('order_by').' '.I('sort');
        $count = $adident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $adident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $loident->getLastSql();
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现添加管理员的功能
     * @author:  XC跃
     * @date :2018-09-28
     +--------------------------------------------------------------------------
     */
    public function adminAddEdit(){
        $id = I('id/d');
        //实例化相关表
        $adident = M('admin');
        $meident = M('menu');
        //实例化相关Model
        $uidentr = new SetupModel();
        if(IS_POST){
            $uid = I("post.uid/d");
            $usFind = $adident->where("`ad_phone`='".I('post.phone')."'")->find();
            if(count($usFind)>0 && $uid!=$usFind['ad_id']){
                $res = array('status'=>-1,'msg'=>'该手机号已存在');
                exit(json_encode($res));
            }
            $re = $uidentr->admin_add_edit($uid);
            if('1'==$re){
                $res = array('status'=>1,'msg'=>'保存成功');
            }else{
                $res = array('status'=>-1,'msg'=>'未做更改');
            }exit(json_encode($res));
        }else{
            $detail = array();
            if($id){
                $ure = $adident->where("`ad_id`='".$id."'")->find();
                $detail['rid'] = explode(',', $ure['ad_rid']);
                $this->assign('ure',$ure);
                $this->assign('act','edit');
            }
            $right = $meident->order('me_id')->select();
            foreach ($right as $val){
                if(!empty($detail)){
                    $val['enable'] = in_array($val['me_id'], $detail['rid']);
                }
                $modules[$val['me_group']][] = $val;
            }
            //dump($modules);
            $this->assign('modules',$modules);
            $this->display();
        }
    }
	/**
	 +--------------------------------------------------------------------------
	 * 实现获取权限列表的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function roleList(){
	    $this->display();
	}
	public function ajaxRoleList(){
	    //实例化相关表
	    $loident = M('menu');
	    // 搜索条件
	    $condition = array();
	    I('rname') ? $condition['me_name'] = array('LIKE',"%".I('rname')."%") :false;
	    I('type') ? $condition['me_group'] = I('type') :false;
	    $sort_order = I('order_by').' '.I('sort');
	    $count = $loident->where($condition)->count();
	    $Page  = new AjaxPage($count,50);
	    $show = $Page->show();
	    $list = $loident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $loident->getLastSql();
	    $this->assign('list',$list);
	    $this->assign('page',$show);// 赋值分页输出
	    $this->assign('rows',$Page->totalRows);
	    $this->display();
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现添加（修改）权限资源的功能
	 * @author:  XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function roleAddEdit(){
	    //实例化相关表
	    $meident = M('menu');
	    if(IS_POST){
	        //$data = I('post.');
	        $rid = I('post.id');
	        $data['me_name']  = $rname = I('post.rname');
	        $data['me_group'] = I('post.group');
	        $data['me_right'] = implode(',',I('post.right'));
	        //dump($data);die;
	        if(!empty($rid)){
	         	$re = $meident->where(array('me_id'=>$rid))->save($data);
	        }else{
	            $count = $meident->where(array('me_name'=>$rname))->count();
	            if($count>0){
	                $res = array('status'=>-1,'msg'=>'该权限名称已添加，请检查');
	                exit(json_encode($res));
     			}
     			unset($rid);
     			$re = $meident->add($data);
	        }
	        if($re>0){
	            $res = array('status'=>1,'msg'=>'操作成功');
	            exit(json_encode($res));
	        }
	    }
	    $id = I('id');
	    if($id){
	        $info = $meident->where(array('me_id'=>$id))->find();
	        $info['right'] = explode(',', $info['me_right']);
	        $this->assign('info',$info);
	    }
	    $planPath = APP_PATH.'Hoteladmin/Controller';
	    $planList = array();
	    $dirRes   = opendir($planPath);
	    while(($dir = readdir($dirRes))!== false)
	    {
	        if(!in_array($dir,array('.','..','.svn')))
	        {
	         	$planList[] = basename($dir,'.class.php');
	        }
	    }
	    //dump($planList);
	    $this->assign('planList',$planList);
	    $this->display();
	}
	//获取某个控制器中的相关函数
	function ajax_get_action()
	{
	    $control = I('controller');
	    $advContrl = get_class_methods("Hoteladmin\\Controller\\".$control);
	    //dump($advContrl);
	    $baseContrl = get_class_methods('Hoteladmin\Controller\BaseController');
	    $diffArray  = array_diff($advContrl,$baseContrl);
	    $html = '';
	    $html .= "<option value=''>请选择方法名</option>";
	    foreach ($diffArray as $val){
	        $html .= "<option value='".$val."'>".$val."</option>";
	    }
	    exit($html);
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现删除无效权限资源
	 * @author:XC跃
	 * @date :2018-09-28
	 +--------------------------------------------------------------------------
	 */
	public function roleDel(){
	    //实例化相关表
	    $meident = M('menu');
	    $re = $meident->where("`me_id`='".I('id/d')."'")->delete();
	    if($re>0){
	        $res = array('status'=>1,'msg'=>'删除成功');
	        exit(json_encode($res));
	    }
	}
}